package com.xbongbong.sso.service.imp;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.EnableEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.pro.enums.errorcodes.SsoErrorCodeEnum;
import com.xbongbong.sso.domain.entity.OauthConnectorEntity;
import com.xbongbong.sso.domain.entity.OauthCustomParamEntity;
import com.xbongbong.sso.enums.OauthFieldEnum;
import com.xbongbong.sso.enums.OauthFunctionEnum;
import com.xbongbong.sso.help.CalculationHelp;
import com.xbongbong.sso.help.OauthCustomParamHelp;
import com.xbongbong.sso.help.OauthParamHelp;
import com.xbongbong.sso.model.OauthConnectorModel;
import com.xbongbong.sso.model.OauthCustomParamModel;
import com.xbongbong.sso.pojo.OauthCustomParamPojo;
import com.xbongbong.sso.pojo.OauthFieldPojo;
import com.xbongbong.sso.pojo.OauthFunctionPojo;
import com.xbongbong.sso.pojo.ParameterMapPojo;
import com.xbongbong.sso.pojo.dto.OauthCustomParamAddDTO;
import com.xbongbong.sso.pojo.dto.OauthCustomParamDeleteDTO;
import com.xbongbong.sso.pojo.dto.OauthCustomParamDetailDTO;
import com.xbongbong.sso.pojo.dto.OauthCustomParamListDTO;
import com.xbongbong.sso.pojo.dto.OauthCustomParamUpdateDTO;
import com.xbongbong.sso.pojo.dto.OauthFieldListDTO;
import com.xbongbong.sso.pojo.dto.OauthSyntaxCheckDTO;
import com.xbongbong.sso.pojo.vo.OauthCustomParamAddVO;
import com.xbongbong.sso.pojo.vo.OauthCustomParamDetailVO;
import com.xbongbong.sso.pojo.vo.OauthCustomParamListVO;
import com.xbongbong.sso.pojo.vo.OauthCustomParamUpdateVO;
import com.xbongbong.sso.pojo.vo.OauthFieldListVO;
import com.xbongbong.sso.pojo.vo.OauthFunctionlistVO;
import com.xbongbong.sso.service.OauthCustomParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 授权自定义参数
 */
@Service("oauthCustomParamService")
public class OauthCustomParamServiceImpl implements OauthCustomParamService {
    private static final Logger LOG = LoggerFactory.getLogger(OauthCustomParamServiceImpl.class);

    @Resource
    private OauthCustomParamModel oauthCustomParamModel;
    @Resource
    private OauthConnectorModel oauthConnectorModel;
    @Resource
    private OauthCustomParamHelp oauthCustomParamHelp;
    @Resource
    private OauthParamHelp oauthParamHelp;
    @Resource
    private CalculationHelp calculationHelp;

    @Override
    public OauthCustomParamListVO list(OauthCustomParamListDTO oauthCustomParamListDTO) throws XbbException {
        String corpid = oauthCustomParamListDTO.getCorpid();
        Long connectorId = oauthCustomParamListDTO.getConnectorId();
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("connectorId", connectorId);
        param.put("del", 0);
        List<OauthCustomParamEntity> oauthCustomParams = oauthCustomParamModel.findEntities(param);
        List<OauthCustomParamPojo> list = new ArrayList<>();
        for (OauthCustomParamEntity oauthCustomParam : oauthCustomParams) {
            list.add(oauthCustomParamHelp.oauthCustomParamPojoFormat(oauthCustomParam));
        }
        OauthCustomParamListVO oauthCustomParamListVO = new OauthCustomParamListVO();
        oauthCustomParamListVO.setList(list);
        return oauthCustomParamListVO;
    }

    @Override
    public OauthFieldListVO fieldlist(OauthFieldListDTO oauthFieldListDTO) throws XbbException {
        List<OauthFieldPojo> systemList = OauthFieldEnum.getSystemList();
        String corpid = oauthFieldListDTO.getCorpid();
        Long connectorId = oauthFieldListDTO.getConnectorId();
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("connectorId", connectorId);
        param.put("enable", EnableEnum.OPEN.getCode());
        param.put("del", 0);
        List<OauthCustomParamEntity> oauthCustomParams = oauthCustomParamModel.findEntities(param);
        List<OauthFieldPojo> customList = new ArrayList<>();
        for (OauthCustomParamEntity oauthCustomParam : oauthCustomParams) {
            customList.add(new OauthFieldPojo(oauthCustomParam.getAttr(), oauthCustomParam.getName(), oauthCustomParam.getDescription()));
        }
        OauthFieldListVO oauthFieldListVO = new OauthFieldListVO();
        oauthFieldListVO.setSystemList(systemList);
        oauthFieldListVO.setCustomList(customList);
        return oauthFieldListVO;
    }

    @Override
    public OauthFunctionlistVO functionlist() throws XbbException {
        List<OauthFunctionPojo> list = new ArrayList<>();
        List<OauthFunctionEnum> oauthFunctionEnums = OauthFunctionEnum.getAll();
        for (OauthFunctionEnum oauthFunctionEnum : oauthFunctionEnums) {
            OauthFunctionPojo oauthFunctionPojo = new OauthFunctionPojo(oauthFunctionEnum.getName(),
                    oauthFunctionEnum.getFunction(), oauthFunctionEnum.getDescription());
            list.add(oauthFunctionPojo);
        }
        OauthFunctionlistVO oauthFunctionlistVO = new OauthFunctionlistVO();
        oauthFunctionlistVO.setList(list);
        return oauthFunctionlistVO;
    }

    @Override
    public BaseVO syntaxCheck(OauthSyntaxCheckDTO oauthSyntaxCheckDTO) throws XbbException {
        OauthConnectorEntity oauthConnector = oauthConnectorModel.getByKey(oauthSyntaxCheckDTO.getConnectorId(),
                oauthSyntaxCheckDTO.getCorpid());
        ParameterMapPojo parameterMapPojo = oauthParamHelp.getParameterMap(oauthConnector);
        Map<String, String> constantParameterMap = parameterMapPojo.getConstantParameterMap();
        Map<String, OauthCustomParamEntity> calculationCustomParameterMap = parameterMapPojo.getCalculationCustomParameterMap();
        String value = calculationHelp.getValue(oauthSyntaxCheckDTO.getValue(), constantParameterMap, calculationCustomParameterMap);
        return new BaseVO();
    }

    @Override
    public OauthCustomParamDetailVO detail(OauthCustomParamDetailDTO oauthCustomParamDetailDTO) throws XbbException {
        String corpid = oauthCustomParamDetailDTO.getCorpid();
        Long id = oauthCustomParamDetailDTO.getId();
        OauthCustomParamEntity oauthCustomParam = oauthCustomParamModel.getByKey(id, corpid);
        OauthCustomParamDetailVO oauthCustomParamDetailVO = new OauthCustomParamDetailVO();
        oauthCustomParamDetailVO.setOauthCustomParam(oauthCustomParamHelp.oauthCustomParamPojoFormat(oauthCustomParam));
        return oauthCustomParamDetailVO;
    }

    @Override
    public OauthCustomParamAddVO add(OauthCustomParamAddDTO oauthCustomParamAddDTO) throws XbbException {
        String corpid = oauthCustomParamAddDTO.getCorpid();
        Long connectorId = oauthCustomParamAddDTO.getConnectorId();
        String name = oauthCustomParamAddDTO.getName();
        String attr = oauthCustomParamAddDTO.getAttr();
        String value = oauthCustomParamAddDTO.getValue();
        Integer enable = oauthCustomParamAddDTO.getEnable();
        String description = oauthCustomParamAddDTO.getDescription();

        OauthCustomParamEntity oauthCustomParam = new OauthCustomParamEntity();
        oauthCustomParam.setCorpid(corpid);
        oauthCustomParam.setConnectorId(connectorId);
        oauthCustomParam.setName(name);
        oauthCustomParam.setAttr(attr);
        oauthCustomParam.setValue(value);
        oauthCustomParam.setEnable(enable);
        oauthCustomParam.setDescription(description);
        oauthCustomParamModel.insert(oauthCustomParam);
        OauthCustomParamAddVO oauthCustomParamAddVO = new OauthCustomParamAddVO();
        oauthCustomParamAddVO.setDataId(oauthCustomParam.getId());
        return oauthCustomParamAddVO;
    }

    @Override
    public OauthCustomParamUpdateVO update(OauthCustomParamUpdateDTO oauthCustomParamUpdateDTO) throws XbbException {
        Long id = oauthCustomParamUpdateDTO.getId();
        String corpid = oauthCustomParamUpdateDTO.getCorpid();
        String name = oauthCustomParamUpdateDTO.getName();
        String attr = oauthCustomParamUpdateDTO.getAttr();
        String value = oauthCustomParamUpdateDTO.getValue();
        Integer enable = oauthCustomParamUpdateDTO.getEnable();
        String description = oauthCustomParamUpdateDTO.getDescription();
        OauthCustomParamEntity oauthCustomParam = new OauthCustomParamEntity();
        oauthCustomParam.setId(id);
        oauthCustomParam.setCorpid(corpid);
        oauthCustomParam.setName(name);
        oauthCustomParam.setAttr(attr);
        oauthCustomParam.setValue(value);
        oauthCustomParam.setEnable(enable);
        oauthCustomParam.setDescription(description);
        oauthCustomParam.setUpdateTime(DateTimeUtil.getInt());
        oauthCustomParamModel.update(oauthCustomParam);
        OauthCustomParamUpdateVO oauthCustomParamUpdateVO = new OauthCustomParamUpdateVO();
        oauthCustomParamUpdateVO.setDataId(oauthCustomParam.getId());
        return oauthCustomParamUpdateVO;
    }

    @Override
    public BaseVO delete(OauthCustomParamDeleteDTO oauthCustomParamDeleteDTO) throws XbbException {
        Long id = oauthCustomParamDeleteDTO.getId();
        String corpid = oauthCustomParamDeleteDTO.getCorpid();
        OauthCustomParamEntity oauthCustomParam = oauthCustomParamModel.getByKey(id, corpid);
        if (Objects.isNull(oauthCustomParam) || !Objects.equals(oauthCustomParam.getDel(), DelEnum.NORMAL.getDel())) {
            throw new XbbException(SsoErrorCodeEnum.API_ERROR_273003);
        }
        oauthCustomParamModel.delete(id, corpid);
        return new BaseVO();
    }
}
